package cn.sxt.bus.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.sxt.bus.domain.Provider;
import cn.sxt.bus.service.ProviderService;
import cn.sxt.bus.vo.ProviderVo;
import cn.sxt.sys.utils.DataGridView;

@Controller
@RequestMapping("provider")
public class ProviderController {

	@Autowired
	private ProviderService providerService;

	// 跳到系统公告管理providerManager.jsp
	@RequestMapping("toProviderManager")
	public String toProviderManager() {
		return "business/provider/providerManager";
	}

	// 登陆系统公告全查询，分页，模糊
	@RequestMapping("queryAllProvider")
	@ResponseBody
	public DataGridView queryAllProvider(ProviderVo providerVo) {
		return providerService.queryAllProvider(providerVo);
	}

	// 跳添加页面
	@RequestMapping("toAddProvider")
	public String toAddProvider() {
		return "business/provider/providerAdd";
	}

	// 添加
	@RequestMapping("addProvider")
	@ResponseBody
	public Map<String, Object> addProvider(ProviderVo providerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = providerService.addProvider(providerVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateProvider")
	public String toUpdateProvider(ProviderVo providerVo, Model model) {
		Provider provider = providerService.queryProviderById(providerVo.getId());
		model.addAttribute("provider", provider);
		return "business/provider/providerUpdate";
	}
	
	// 修改
	@RequestMapping("updateProvider")
	@ResponseBody
	public Map<String, Object> updateProvider(ProviderVo providerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = providerService.updateProvider(providerVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除系统公告
	@RequestMapping("deleteProvider")
	@ResponseBody
	public Map<String, Object> deleteProvider(ProviderVo providerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = providerService.deleteProviderById(providerVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除系统公告
	@RequestMapping("batchDeleteProvider")
	@ResponseBody
	public Map<String, Object> batchDeleteProvider(ProviderVo providerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = providerVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					providerVo.setId(ids[i]);
					int j = providerService.deleteProviderById(providerVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}
}
